/* eslint-disable no-use-before-define */
// eslint-disable-next-line import/prefer-default-export
export class IndexService {
  // constructor() { }

  // eslint-disable-next-line class-methods-use-this
  verifyStep(activeIndex, formData) {
    let result = {};
    let rules = [];
    switch (activeIndex) {
      case 1:
        rules = [
          { rule: formData.supplier_id !== '', message: '请选择供应商' },
          { rule: formData.contract_id !== '', message: '请选择合同' }
        ];
        result = checkRule(rules);
        break;
      case 2:
        if (formData.busModel === '2') {
          rules = [
            {
              rule: formData.return_type_name !== '',
              message: '请选择退货类型'
            },
            {
              rule: formData.return_reason.reason_sel !== '',
              message: '请选择换货原因'
            },
            {
              rule: formData.return_reason.reason_text !== '',
              message: '请填写具体原因'
            },
            {
              rule: formData.return_reason.reason_imgs.length > 0,
              message: '请选择图片'
            },
            { rule: formData.return_goods.length > 0, message: '请添加产品' }
          ];
        } else {
          rules = [
            {
              rule: formData.return_reason.reason_sel !== '',
              message: '请选择换货原因'
            },
            {
              rule: formData.return_reason.reason_text !== '',
              message: '请填写具体原因'
            },
            {
              rule: formData.return_reason.reason_imgs.length > 0,
              message: '请选择图片'
            },
            { rule: formData.return_goods.length > 0, message: '请添加产品' }
          ];
        }
        result = checkRule(rules);
        break;
      case 3:
        rules = [
          {
            rule: formData.exchange_goods.length > 0,
            message: '请添加换货产品'
          }
        ];
        result = checkRule(rules);
        break;
      case 4:
        rules = [
          {
            rule:
              formData.full_address &&
              formData.full_address.id &&
              formData.full_address.id !== '',
            message: '请选择退货地址'
          },
          { rule: formData.carryway_id !== '', message: '请选择承运方式' }
        ];
        result = checkRule(rules);
        if (result.pass === false) {
          return result;
        }
        break;
      default:
        result.pass = true;
        break;
    }
    return result;
  }
}

/**
 * 检查规则，遇到rule=false的情况，立即扔出结果
 * @param {rule:bool, message:string} verifyRules
 */
function checkRule(verifyRules) {
  const result = { pass: true, message: '' };
  // eslint-disable-next-line no-plusplus
  for (let i = 0; i < verifyRules.length; i++) {
    const element = verifyRules[i];
    if (element.rule === true) {
      result.pass = true;
    } else {
      result.message = element.message;
      result.pass = false;
      return result;
    }
  }
  return result;
}
